package net.dgg.zz.api.credential.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.api.credential.entity.CertificateProductConfiguration;
import net.dgg.zz.api.credential.service.CertificateProductConfigurationService;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.credential.certificateMatching.entity.CertificateMatchingEntity;
import net.dgg.zz.credential.certificateType.entity.CertificateType;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zz/zspz")
public class CertificateProductConfigurationController extends DggBaseController {
    @Autowired
    private CertificateProductConfigurationService certificateProductConfigurationService;

    @Autowired
    private SessionManager sessionManager;

    /**
     * 保存配置
     * @param params
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public DggRestResponse createNewMatchingSave(@RequestParam Map params) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        try {
            certificateProductConfigurationService.insert(params,userEntity);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }

    /**
     * 删除配置
     * @param id
     * @return
     */
    @RequestMapping(value="/delete", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse deleteCertificateMatchingById(@RequestParam Long id){
        try{
           certificateProductConfigurationService.deleteById(id);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }


    /**
     * 分页查询所有
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public DataTableResponse queryCertificateProductConfiguration(@RequestParam Map<String, Object> params, HttpServletRequest request){
        DataTableRequest r = DataTableUtils.getParam(request);
        params.put("start", (r.getPage() - 1) * r.getLength());
        params.put("limit", r.getLength());
        List<CertificateProductConfiguration> list  =certificateProductConfigurationService.getCertificateMatchList(params);
        Integer count = certificateProductConfigurationService.count();
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }

    /**
     * 修改是否禁用
     * @param params
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public DggRestResponse editSave(@RequestParam Map params) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            certificateProductConfigurationService.updateCertificateMatching(params,userEntity);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }
}
